/**
 * F公共业务逻辑
 */
XX.C = {};
var r = {
    protocol: /([^\/]+:)\/\/(.*)/i,
    host: /(^[^\:\/]+)((?:\/|:|$)?.*)/,
    port: /\:?([^\/]*)(\/?.*)/,
    pathname: /([^\?#]+)(\??[^#]*)(#?.*)/
};

function parseURL(url) {
    var tmp, res = {};
    res.href = url;
    for (var p in r) {
        tmp = r[p].exec(url);
        res[p] = tmp[1];
        url = tmp[2];
        if (url === "") {
            url = "/";
        }
        if (p === "pathname") {
            res.pathname = tmp[1];
            res.search = tmp[2];
            res.hash = tmp[3];
        }
    }
    return res;
}

$$.extend(XX.C, {

    // 针对iphone iPad 输入框不失焦
    fixedInputBlur: function(){
        if($$.isiPhone || $$.isiOS || $$.isiPad){
            $('body').on('touchstart.inputBlur',function(e){
                var target = e.srcElement || e.target;
                if(!$(target).hasClass("textfield-clean")){
                    $('.textfield.active').find("input").blur();
                }
            });
        }
    },
    // 退出登录
    Logout: function(){
        var $_ = this,
            isMain = false,
            mainModule = [
                'main/homePage.html',
                'main/bank.html',
                'main/invest.html',
                'main/life.html'
            ];
        XX.C.getUserInfo(function(d){
            if(!_.isEmpty(d)){
                if(d.LoginType === 'D'){
                    $$.Native.updateTab([{id:'BANK',text:'网银'}]);
                }

                F().callbacks.C_LoginOut = function(){

                };

                F().request({
                    'url': 'PORTAL_SMBP_ESB',
                    'data': {
                        'ffFunctionCode':'TZB_LOGIN_004',
                        'donotForwardLogin':'yes'
                    },
                    'success': 'C_LoginOut'
                });

                $$.Native.clearAppointData({
                    data: ['ANY_DOOR_DATA'],
                    callback: function(){
                        $$.Native.logoutCallAnyDoor();
                        _.each(mainModule, function(item){
                            if(location.href.indexOf(item)!== -1){
                                $$.Native.backToRootModule({moduleId:'HOME'});
                                isMain = true;
                            }
                        });

                        if(!isMain){
                            $$.Native.back({url:'main/homePage.html'});
                        }
                    }
                });
            }else{
                _.each(mainModule, function(item){
                    if(location.href.indexOf(item)!== -1){
                        $$.Native.backToRootModule({moduleId:'HOME'});
                        isMain = true;
                    }
                });

                if(!isMain){
                    $$.Native.back({url:'main/homePage.html'});
                }
            }
        });
    },
    //设置用户登录信息
    setUserLoginType: function (data, callback) {
        $$.Native.setData({
            name: '__user',
            value: data,
            callback: function (d) {
                if (typeof callback === "function") {
                    callback(d);
                }
            }
        });
    },
    //获取用户登录信息
    getUserLoginType: function (callback) {

        $$.Native.getData({
            name: '__user',
            callback: function (d) {
                if (typeof callback === "function") {
                    callback(d);
                }
            }
        });
    },
    //设置用户信息
    setUserInfo: function (data, callback) {
        $$.Native.setData({
            name: '__userInfo',
            value: data,
            callback: function (d) {
                if (typeof callback === "function") {
                    callback(d);
                }
            }
        });
    },
    //获取用户信息
    getUserInfo: function (callback) {
        $$.Native.getData({
            name: '__userInfo',
            callback: function (d) {
                if (typeof callback === "function") {
                    callback(d);
                }
            }
        });
    },
    // 设置默认头部
    setDefaultHeader: function () {
        var title = $.trim($('title').text()),
            list = [
                "modules/html/main/homePage.html"
            ],
            flag = true;
        //过滤首页模块，不显示title
        _.each(list, function (item) {
            if (location.href.split("?")[0].indexOf(item) != "-1") {
                flag = false;
                return;
            }
        });
        var loginForwardbackUrl = $$.getQueryString("loginForwardbackUrl");
        if (flag) {
            $$.Native.setHeader({
                title: title || "",
                isBack: true,
                backCallback: function () {
                    var options = "";
                    if(loginForwardbackUrl){
                        options = {
                            url:loginForwardbackUrl
                        };
                    }
                    $$.Native.back(options);
                }
            });
        }
    },

    // 获取设备平台 是否IOS Android?
    getMobileDevice: function () {
        if ($$.isAndroid()) {
            return '01';
        }

        if ($$.isiOS()) {
            return '02';
        }

        if ($$.isiPad()) {
            return '03';
        }
    },

    /*
     * 拦截需要拦截入口页面
     * 跳转的url
     * 跳转的标题
     */
    loginFilter: function (url, title, callback) {
        callback();  
    }
});

XX.C.setDefaultHeader();


window.onerror = function (msg, file, line) {
    console.log(msg);
    console.log(file);
    console.log(line);
};


